@page "/user"
@using BootBlazor.Servers.Auth
@using BootBlazor.Servers.Components.Pages
@using BootBlazor.Servers.Helper
@using BootBlazor.Servers.Data
@using BootBlazor.Servers.Services

@rendermode RenderMode.InteractiveServer
@attribute [StreamRendering]

<PageHeader Title="@Loc["UserPage_Title"]">
    <MudGrid Spacing="2" Class="justify-end">
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudTextField T="string" @bind-Value="searchObject.SearchText" Margin="Margin.Dense" Label="@Loc["UserPage_SearchName"]"
                          Variant="Variant.Outlined" Clearable Class="search-com"></MudTextField>
        </MudItem>
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudTextField T="string" @bind-Value="searchObject.SearchRealName" Margin="Margin.Dense" Label="@Loc["UserPage_SearchRealName"]"
                          Variant="Variant.Outlined" Clearable Class="search-com"></MudTextField>
        </MudItem>
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <MudSelect T="string" @bind-Value="searchObject.SearchRole" Margin="Margin.Dense" Label="@Loc["UserPage_SearchRole"]"
                       Variant="Variant.Outlined" Clearable Class="search-com">
                @foreach (var r in RoleList)
                {
                    <MudSelectItem Value="@r.Id.ToString()">@r.Name</MudSelectItem>
                }
            </MudSelect>
        </MudItem>
        <MudItem xs="12" sm="6" md="3" lg="2" xl="1">
            <OrganizationTreeSelect Label="@Loc["UserPage_SearchOrg"]" ShowIcon="false"
                                    @bind-SelectedValue="searchObject.SelectedOrganization" />
        </MudItem>
        <MudItem>
            <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.Search" OnClick="() => { dataGrid.ReloadServerData(); }"
                           Variant="Variant.Outlined" Color="Color.Primary" Class="mr-2">
            </MudIconButton>
            <MudTooltip Text="重置搜索" Color="Color.Primary">
                <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.SearchOff" OnClick="SearchReset"
                               Variant="Variant.Outlined" Color="Color.Primary" Class="mr-2"></MudIconButton>
            </MudTooltip>
            @if (_accessService.CheckHasElementRights("UserAddBtn").Result)
            {
                <MudIconButton Size="MudBlazor.Size.Medium" Icon="@Icons.Material.Filled.Add" OnClick="AddUserClick"
                               Variant="Variant.Outlined" Color="Color.Primary">
                </MudIconButton>
            }
        </MudItem>
    </MudGrid>
</PageHeader>


<MudDataGrid Dense=PageDataGridConfig.Dense Filterable=PageDataGridConfig.Filterable ColumnResizeMode=PageDataGridConfig.ColumnResizeMode
             SortMode=PageDataGridConfig.SortMode Groupable=PageDataGridConfig.Groupable Virtualize=PageDataGridConfig.Virtualize
             FixedHeader=PageDataGridConfig.FixedHeader Elevation=PageDataGridConfig.Elevation Outlined=PageDataGridConfig.Outlined
             Style="@PageDataGridConfig.Style" HorizontalScrollbar="PageDataGridConfig.HorizontalScrollbar"
             ServerData="GetTableData" @ref="dataGrid" T="UserModel">
    <Columns>
        <PropertyColumn Property="x => x.Number" Title="@Loc["UserPage_TableH1"]" StickyLeft="true" Resizable="false" HeaderStyle="white-space:nowrap;width:60px;" />
        <TemplateColumn Title="@Loc["UserPage_TableH2"]" HeaderStyle="white-space:nowrap;">
            <CellTemplate>
                @if (string.IsNullOrEmpty(context.Item.Avatar))
                {
                    <MudAvatar Color="Color.Primary">
                        @context.Item.RealName?.First()
                    </MudAvatar>
                }
                else
                {
                    <MudAvatar>
                        <MudImage Src="@("Avatars/" + context.Item.Avatar)">
                        </MudImage>
                    </MudAvatar>
                }
            </CellTemplate>
        </TemplateColumn>
        <PropertyColumn Property="x => x.Name" Title="@Loc["UserPage_TableH3"]" HeaderStyle="white-space:nowrap" />
        <PropertyColumn Property="x => x.RealName" Title="@Loc["UserPage_TableH6"]" HeaderStyle="white-space:nowrap" />
        <PropertyColumn Property="x => x.Email" Title="@Loc["UserPage_TableH9"]" HeaderStyle="white-space:nowrap" />
        <PropertyColumn Property="x => x.PhoneNumber" Title="@Loc["UserPage_TableH10"]" HeaderStyle="white-space:nowrap" />
        <TemplateColumn Title="@Loc["UserPage_TableH7"]" HeaderStyle="white-space:nowrap">
            <CellTemplate>
                @foreach (var role in context.Item.Roles)
                {
                    <MudChip Size="MudBlazor.Size.Medium" Color="Color.Primary" Variant="Variant.Outlined">@role</MudChip>
                }
            </CellTemplate>
        </TemplateColumn>
        <TemplateColumn Title="@Loc["UserPage_TableH8"]" HeaderStyle="white-space:nowrap">
            <CellTemplate>
                @foreach (var organization in context.Item.Organizations)
                {
                    <MudChip Size="MudBlazor.Size.Medium" Color="Color.Primary" Variant="Variant.Outlined">@organization</MudChip>
                }
            </CellTemplate>
        </TemplateColumn>
        <TemplateColumn Title="@Loc["UserPage_TableH4"]" HeaderStyle="white-space:nowrap;">
            <CellTemplate>
                <MudSwitch Value="@context.Item.IsEnabled" Disabled="@(!_accessService.CheckHasElementRights("UserActiveBtn").Result)" T="bool"
                           ValueChanged="v => ChangeUserActive(context.Item.Id, v)" Color="Color.Primary">
                </MudSwitch>
            </CellTemplate>
        </TemplateColumn>
        <TemplateColumn Title="@Loc["UserPage_TableH5"]" HeaderStyle="white-space:nowrap;width:140px;min-width:140px;" StickyRight="true" Resizable="false">
            <CellTemplate>
                @if (_accessService.CheckHasElementRights("UserUpdateBtn").Result)
                {
                    <MudIconButton Size="@MudBlazor.Size.Small" Icon="@Icons.Material.Outlined.Edit" Color="Color.Primary"
                                   OnClick="() => EditUserClick(context.Item.Id)" />
                }
                @if (_accessService.CheckHasElementRights("UserPwdChangeBtn").Result)
                {
                    <MudIconButton Size="@MudBlazor.Size.Small" Icon="@Icons.Material.Outlined.Key" Color="Color.Secondary"
                                   OnClick="() => ChangePasswordClick(context.Item.Id)" />
                }
                @if (_accessService.CheckHasElementRights("UserRoleSettingBtn").Result)
                {
                    <MudIconButton Size="@MudBlazor.Size.Small" Icon="@Icons.Material.Outlined.LockPerson" Color="Color.Tertiary"
                                   OnClick="() => SetUserRoleClick(context.Item.Id)" />
                }
                @if (_accessService.CheckHasElementRights("UserDeleteBtn").Result)
                {
                    <MudIconButton Size="@MudBlazor.Size.Small" Icon="@Icons.Material.Outlined.Delete" Color="Color.Error"
                                   OnClick="() => DeleteUserClick(context.Item.Id)" />
                }
            </CellTemplate>
        </TemplateColumn>
    </Columns>
</MudDataGrid>

<PagePagination PageInfo="searchObject" PageChangedClick="PageChangedClick"></PagePagination>
